package com.zb.system.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.result.Result;
import com.zb.service.ZjOrganizeService;
import com.zb.utils.TokenUtil;
import com.zb.vo.OrganizeVo;
import com.zb.vo.OrganizesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商家组织表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-07-18
 */
@Api(tags = "组织类接口")
@RestController
@RequestMapping("/system/organize")
public class ZjOrganizeController {
    @Resource
    private ZjOrganizeService organizeService;

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "组织 - 新增或修改组织及组织人员")
    @ApiOperation("新增或修改组织及组织人员")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid OrganizeVo organizeVo) {
        AccounLoginInfo accounLoginInfo = TokenUtil.getAccountLoginInfo(request);
        organizeService.save(accounLoginInfo, organizeVo);
        return Result.success(null == organizeVo.getId()? "新增组织成功" : "修改组织成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.SELECT, remark = "组织 - 获取商家所有组织列表")
    @ApiOperation("获取商家所有组织")
    @GetMapping("/getList")
    public Result getList(HttpServletRequest request) {
        AccounLoginInfo accounLoginInfo = TokenUtil.getAccountLoginInfo(request);
        List<OrganizesVo> organizesVoList = organizeService.getList(accounLoginInfo.getCustAccId());
        return Result.success("获取所有组织成功！").data("rows", organizesVoList);
    }

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.SELECT, remark = "组织 - 根据组织ID获取组织信息")
    @ApiOperation("根据组织ID获取组织信息")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable @ApiParam(value = "组织ID", required = true) Long id) {
        return Result.success("获取组织信息成功").data("row", organizeService.getInfo(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "组织 - 禁用或启用组织")
    @ApiOperation("禁用或启用组织")
    @PutMapping("/disabled")
    public Result disabled(@RequestParam @ApiParam(value = "是否禁用：false-启用，true-禁用", required = true, example = "false") boolean status,
                           @RequestBody @ApiParam(value = "组织ID集合", required = true) List<Long> idList) {
        organizeService.updateBatchDisabledById(idList, status);
        return Result.success();
    }

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.DELETE, remark = "组织 - 删除组织")
    @ApiOperation("删除组织")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody @ApiParam(value = "组织ID集合", required = true) List<Long> idList) {
        organizeService.deleteBatchById(idList);
        return Result.success("删除组织成功!");
    }



}
